package com.mateng.treasurechest.base

import android.app.Activity
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.mateng.treasurechest.R
import com.mateng.treasurechest.presenter.BasePresenter
import com.mateng.treasurechest.view.common.weight.NetProgressView
import java.lang.reflect.ParameterizedType


/**
 * Created by mateng on 2018/1/24.
 */
abstract class BaseActivity<T : BasePresenter> : Activity() {


    protected val TAG = this::class.java.simpleName
    protected var presenter: T? = null

    private var contentLayout: LinearLayout? = null
    private var contentView: View? = null
    private var actionBar: View? = null
    private var netProgress: View? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val type = this.javaClass.genericSuperclass as ParameterizedType
        val clazz: Class<T>? = type.actualTypeArguments[0] as Class<T>?
        presenter = Class.forName(clazz?.canonicalName).newInstance() as T?

        initContentView()
        actionBar = setActionBar(contentLayout)
        contentLayout?.addView(actionBar)

        netProgress = getNetProgress()

        ActivityManager.instance.addActivity(this)
    }

    override fun onDestroy() {
        ActivityManager.instance.removeActivity(this)
        presenter?.recycler()
        super.onDestroy()
    }

    private fun initContentView() {
        val content: ViewGroup = findViewById(android.R.id.content)
        content.removeAllViews()
        contentLayout = LinearLayout(this)
        contentLayout?.orientation = LinearLayout.VERTICAL
        content.addView(contentLayout)
    }

    final override fun setContentView(layoutResID: Int) {
        contentView = LayoutInflater.from(this).inflate(layoutResID, contentLayout, false)
        contentLayout?.addView(contentView)
    }

    final override fun setContentView(view: View?) {
        contentView = view
        contentLayout?.addView(view)
    }

    /**
     * 加载DataBinding布局
     */
    fun <DataBinding : ViewDataBinding> setContentViewByBinding(layoutResID: Int): DataBinding {
        val binding: DataBinding = DataBindingUtil.inflate(LayoutInflater.from(this), layoutResID, contentLayout, false)
        contentView = binding.root
        contentLayout?.addView(contentView)
        return binding
    }

    /**
     * 设置默认网络加载进度条，可重写
     */
    open fun getNetProgress(): View {
        val view = NetProgressView(this)
        val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        view.layoutParams = params
        return view
    }

    /**
     * 设置ActionBar
     */
    open fun setActionBar(parent: ViewGroup?): View {
        val view: View = LayoutInflater.from(this).inflate(R.layout.common_title_bar, parent, false)
        view.findViewById<TextView>(R.id.tv_title).text = title
        view.findViewById<ImageView>(R.id.iv_finish).visibility = View.GONE
        return view
    }

    protected fun showBackIcon() {

        findViewById<ImageView>(R.id.iv_finish)?.visibility = View.VISIBLE

        findViewById<ImageView>(R.id.iv_finish)?.setOnClickListener { finish() }

    }

    protected fun netConnectFailed() {
        contentLayout?.removeAllViews()
        contentLayout?.addView(actionBar)
        LayoutInflater.from(this).inflate(R.layout.net_connect_failed, contentLayout, true)
    }

    protected fun showProgress() {
        contentView?.visibility = View.GONE
        contentLayout?.addView(netProgress)
    }

    protected fun hideProgress() {
        contentView?.visibility = View.VISIBLE
        contentLayout?.removeView(netProgress)
    }

}